package com.example.easyutils.logistics;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.easyutils.BaseActivity;
import com.example.easyutils.R;
import com.example.easyutils.httputil.HttpGetRequest;
import com.example.easyutils.musicpage.MusicAdapter;
import com.example.easyutils.musicpage.MusicType;
import com.example.easyutils.util.DialogLoad;
import com.example.easyutils.util.InputDel;
import com.example.easyutils.util.MapToString;
import com.example.easyutils.util.MyTypeAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import org.w3c.dom.Text;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

public class TraceActivity extends BaseActivity {

    DialogLoad dialogLoad;
    private TextView name;
    private EditText editText;
    private ImageView delImageView;
    private ListView lvTrace;
    private Map traceList;
    private TraceListAdapter adapter;
    private List mList, queryList;
    private static final int SUCCESS_CODE = 1, ERROR_CODE = 2, INIT_CODE = 3, WARM_CODE = 4;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            dialogLoad.dialogCancel();
            switch (msg.what) {
                case SUCCESS_CODE:

                    mList = new ArrayList();
                    mList = (List) traceList.get("info");

                    Collections.reverse(mList);//倒序
                    adapter = new TraceListAdapter(TraceActivity.this, mList);
                    lvTrace.setAdapter(adapter);
                    initData(traceList.get("com").toString());//快递中文转换
//                    initTraceCode(traceList.get("com").toString());

                    break;
                case INIT_CODE:

                    break;
                case ERROR_CODE:
                    Toast.makeText(TraceActivity.this, "出错啦", Toast.LENGTH_LONG).show();
                    break;
                case WARM_CODE:
                    Toast.makeText(TraceActivity.this, "每天只能使用该接口100次", Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.trace_layout);
        editText = (EditText) findViewById(R.id.query_edit);
        name = (TextView) findViewById(R.id.name);
        InputDel inputDel = new InputDel(editText, delImageView, TraceActivity.this);
        inputDel.initListener();
        inputDel.editDel();
        dialogLoad = new DialogLoad(TraceActivity.this);
        findView();


    }


    private void initTraceCode(String number) {//转换快递为中文

        for (int i = 0; i < queryList.size(); i++) {
            String codeTxt = ((Map) queryList.get(i)).get("code").toString();
            String nameTxt = ((Map) queryList.get(i)).get("name").toString();
            if (number.equals(codeTxt)) {
                name.setText(nameTxt);
            }
        }
    }

    private void findView() {
        lvTrace = (ListView) findViewById(R.id.lvTrace);
    }

    public void queryTrace(View v) {//点击查询
        lvTrace.setAdapter(null);//清空物流
        name.setText("");//清空快递
        String number = editText.getText().toString();
        if (number.equals("")) {
            Toast.makeText(TraceActivity.this, "快递单号不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        dialogLoad.dialogLoaded("加载中...");
        HttpGetRequest.sendOkHttpGetRequst("https://v1.alapi.cn/api/kd?number=" + number,
                new okhttp3.Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.d("ERROR_QUERY", e + "");
                        handler.sendEmptyMessage(ERROR_CODE);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        Log.d("SUCCESS_QUERY", result);
                        Gson gson = new GsonBuilder()
                                .registerTypeAdapter(new TypeToken<Map<String, Object>>() {

                                }.getType(), new MyTypeAdapter()).create();
                        Map<String, Object> dataDetail = gson.fromJson(result, new TypeToken<Map<String, Object>>() {

                        }.getType());

                        traceList = new HashMap();
//                        if(code.equals("200")){

                        Long code = (Long) dataDetail.get("code");

                        Log.d("codede", code + "");

                        if (code == 200) {
                            traceList = (Map) dataDetail.get("data");
                            Log.d("200", 200 + "");
                            handler.sendEmptyMessage(SUCCESS_CODE);
                        } else {
                            handler.sendEmptyMessage(ERROR_CODE);
                        }

//                        }else{
//                            handler.sendEmptyMessage(ERROR_CODE);
//                        }

                    }
                });
//        https://v1.alapi.cn/api/kd?number=557027400699112

    }

    private void initData(String str) {
        switch (str) {
            case "zto":
                name.setText("中通快递");
                break;
            case "sto":
                name.setText("申通快递");
                break;
            case "yto":
                name.setText("圆通快递");
                break;
            case "tiantian":
                name.setText("天天快递");
                break;
            case "ems":
                name.setText("EMS快递");
                break;
            case "yunda":
                name.setText("韵达快递");
                break;
            case "yousu":
                name.setText("优速快递");
                break;
            case "800best":
                name.setText("百世快运");
                break;
            case "huitong":
                name.setText("百世汇通快递");
                break;
            case "zhaijisong":
                name.setText("宅急送快递");
                break;
            case "longbang":
                name.setText("龙邦快递");
                break;
            case "suning":
                name.setText("苏宁快递");
                break;
            case "guotong":
                name.setText("国通快递");
                break;
            case "jingdong":
                name.setText("京东快递");
                break;
        }
//https://v1.alapi.cn/api/kd/com  快递公司
//        HttpGetRequest.sendOkHttpGetRequst("https://v1.alapi.cn/api/kd/com",
//                new okhttp3.Callback() {
//                    @Override
//                    public void onFailure(Call call, IOException e) {
//                        handler.sendEmptyMessage(ERROR_CODE);
//                    }
//
//                    @Override
//                    public void onResponse(Call call, Response response) throws IOException {
//
//                        String queryStr = response.body().string();
//                        Gson gson = new GsonBuilder()
//                                .registerTypeAdapter(new TypeToken<Map<String, Object>>(){
//
//                                }.getType(),new MyTypeAdapter()).create();
//                        Map<String,Object> dataDetail = gson.fromJson(queryStr,new TypeToken<Map<String,Object>>(){
//
//                        }.getType());
//                        queryList = new ArrayList();
//
//                        Log.d("data",dataDetail+"");
//                        Long code = (Long) dataDetail.get("code");
//                        if(code==200){
//                            queryList = (List) dataDetail.get("data");
//                            handler.sendEmptyMessage(INIT_CODE);
//                        }else if(code==429){
//                            handler.sendEmptyMessage(WARM_CODE);
//                        }else{
//                            handler.sendEmptyMessage(ERROR_CODE);
//                        }
//
//                    }
//                });

    }

}